
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
    <title>UCTB.train.EarlyStopping &#8212; UCTB  documentation</title>
    <link rel="stylesheet" href="../../../_static/nature.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">UCTB  documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for UCTB.train.EarlyStopping</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">scipy</span> <span class="k">import</span> <span class="n">stats</span>


<div class="viewcode-block" id="EarlyStopping"><a class="viewcode-back" href="../../../UCTB.train.html#UCTB.train.EarlyStopping.EarlyStopping">[docs]</a><span class="k">class</span> <span class="nc">EarlyStopping</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Early stop if a span of newest records are not better than the current best record.</span>

<span class="sd">    Args:</span>
<span class="sd">        patience (int): The span of checked newest records.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        __record_list (list): List of records.</span>
<span class="sd">        __best (float): The current best record.</span>
<span class="sd">        __patience (int): The span of checked newest records.</span>
<span class="sd">        __p (int): The number of newest records that are worse than the current best record.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">patience</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__record_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__best</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__patience</span> <span class="o">=</span> <span class="n">patience</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__p</span> <span class="o">=</span> <span class="mi">0</span>

<div class="viewcode-block" id="EarlyStopping.stop"><a class="viewcode-back" href="../../../UCTB.train.html#UCTB.train.EarlyStopping.EarlyStopping.stop">[docs]</a>    <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new_value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Append the new record to the record list</span>
<span class="sd">        and check if the number of new records than are worse than the best records exceeds the limit.</span>

<span class="sd">        Args:</span>
<span class="sd">            new_value (float): The new record generated by the newest model.</span>

<span class="sd">        Returns:</span>
<span class="sd">            bool: ``True`` if the number of new records than are worse than the best records exceeds the limit and</span>
<span class="sd">            triggers early stop, otherwise ``False``.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__record_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_value</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__best</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">new_value</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">__best</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__best</span> <span class="o">=</span> <span class="n">new_value</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__p</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">__p</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">__patience</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">__p</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="k">return</span> <span class="kc">False</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span></div></div>


<div class="viewcode-block" id="EarlyStoppingTTest"><a class="viewcode-back" href="../../../UCTB.train.html#UCTB.train.EarlyStopping.EarlyStoppingTTest">[docs]</a><span class="k">class</span> <span class="nc">EarlyStoppingTTest</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Early Stop by t-test.</span>

<span class="sd">    T-test is a two-sided test for the null hypothesis that 2 independent samples</span>
<span class="sd">    have identical average (expected) values. This method takes two intervals according to ``length``</span>
<span class="sd">    in the record list and see if they have identical average values. If so, do early stop.</span>

<span class="sd">    Args:</span>
<span class="sd">        length (int): The length of checked interval.</span>
<span class="sd">        p_value_threshold (float): The p-value threshold to decide whether to do early stop.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        __record_list (list): List of records.</span>
<span class="sd">        __best (float): The current best record.</span>
<span class="sd">        __test_length (int): The length of checked interval.</span>
<span class="sd">        __p_value_threshold (float): The p-value threshold to decide whether to do early stop.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">length</span><span class="p">,</span> <span class="n">p_value_threshold</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__record_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__best</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__test_length</span> <span class="o">=</span> <span class="n">length</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__p_value_threshold</span> <span class="o">=</span> <span class="n">p_value_threshold</span>

<div class="viewcode-block" id="EarlyStoppingTTest.stop"><a class="viewcode-back" href="../../../UCTB.train.html#UCTB.train.EarlyStopping.EarlyStoppingTTest.stop">[docs]</a>    <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new_value</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Take two intervals in the record list to do t-test.</span>

<span class="sd">        Args:</span>
<span class="sd">            new_value (float): The new record generated by the newest model.</span>

<span class="sd">        Returns:</span>
<span class="sd">            bool: ``True`` if p value of t-test is smaller than threshold and</span>
<span class="sd">            triggers early stop, otherwise ``False``.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__record_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_value</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__record_list</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__test_length</span> <span class="o">*</span> <span class="mi">2</span><span class="p">):</span>
            <span class="n">lossTTest</span> <span class="o">=</span> <span class="n">stats</span><span class="o">.</span><span class="n">ttest_ind</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__record_list</span><span class="p">[</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">__test_length</span><span class="p">:],</span>
                                        <span class="bp">self</span><span class="o">.</span><span class="n">__record_list</span><span class="p">[</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">__test_length</span> <span class="o">*</span> <span class="mi">2</span><span class="p">:</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">__test_length</span><span class="p">],</span> <span class="n">equal_var</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
            <span class="n">ttest</span> <span class="o">=</span> <span class="n">lossTTest</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">pValue</span> <span class="o">=</span> <span class="n">lossTTest</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;ttest:&#39;</span><span class="p">,</span> <span class="n">ttest</span><span class="p">,</span> <span class="s1">&#39;pValue&#39;</span><span class="p">,</span> <span class="n">pValue</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">pValue</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">__p_value_threshold</span> <span class="ow">or</span> <span class="n">ttest</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">UCTB  documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2019, Di Chai, Leye Wang, Jin Xu, Wenjie Yang, Liyue Chen.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 2.2.1.
    </div>
  </body>
</html>